package com.buyouchina.web.action.product;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.buyouchina.bean.area.Province;
import com.buyouchina.bean.privilege.User;
import com.buyouchina.bean.product.OrderForm;
import com.buyouchina.bean.product.Statistical;
import com.buyouchina.bean.product.Statistical.StatisticalType;
import com.buyouchina.bean.product.Statistical.TongjiAreaType;
import com.buyouchina.service.area.ProvinceService;
import com.buyouchina.service.product.OrderService;
import com.buyouchina.service.product.StatisticalService;
import com.buyouchina.utils.Pagination;
import com.buyouchina.utils.ProvinceUtil;
import com.buyouchina.utils.UserUtil;
import com.buyouchina.web.action.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

@Controller("StatisticalAction")
public class StatisticalAction extends BaseAction implements
		ModelDriven<Statistical>, Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Statistical statistical = new Statistical();

	@Resource(name = "statisticalServiceBean")
	private StatisticalService statisticalService;
	@Resource(name = "orderServiceBean")
	private OrderService orderService;
	@Resource(name = "provinceServiceBean")
	private ProvinceService provinceService;

	private List<Statistical> statisticalList = new ArrayList<Statistical>();
	private List<Province> provinceList = new ArrayList<Province>();
	private List<OrderForm> orderList = new ArrayList<OrderForm>();

	private Integer statisticalid;
	private Integer provinceid;
	private Integer productid;
	private String startTime;
	private String endTime;
	private String productName;

	public String getSt() throws ParseException {

		statistical.setTotalOrder(orderService.findByStatus("交易成功").size());

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String startTime = sdf.format(new Date()) + " 00:00:00";
		String endTime = sdf.format(new Date()) + " 23:59:59";
		statistical.setTodayOrder(orderService.findByDate(
				sdf1.parse(startTime), sdf1.parse(endTime)).size());
		statisticalService.save(statistical);
		return SUCCESS;
	}

	// 进货统计old
	public String list() throws ParseException {
		Integer epid;
		User user = (User) session.get("user");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (startTime == null) {
			startTime = sdf.format(new Date());
		}
		if (endTime == null) {
			endTime = sdf.format(new Date());
		}
		startTime += " 00:00:00";
		endTime += " 23:59:59";
		Pagination pg = new Pagination(request, response);

		if (provinceid != null) {
			
			Province province=provinceService.find(provinceid);
			if(province.getProvinceid()!=null){
				epid=ProvinceUtil.getLastIndexOfCity(provinceService, provinceid);
			}else if(province.getFullAddress()==null){
				epid=ProvinceUtil.getLastIndexOfProvince(provinceService, provinceid);
			}else{
				epid=provinceid;
			}
			pg.setRecordCount(statisticalService.getTotalCount(
					sdf1.parse(startTime), sdf1.parse(endTime), statistical
							.getType(), provinceid,epid));
			statisticalList = statisticalService.list(sdf1.parse(startTime),
					sdf1.parse(endTime), statistical.getType(), provinceid,
					epid,productName, pg.getFirstResult(), pg.getPageSize());
			if (UserUtil.getMaxLevel(user) > 7000) {
				provinceList = provinceService.list();
			} else {
				List<Province> provinces = new ArrayList<Province>(user
						.getProvinces());
				provinceList = provinceService.list(provinces);
			}
		} else {
			try {
				if (UserUtil.getMaxLevel(user) > 7000) {
					pg.setRecordCount(statisticalService.getCount(sdf1
							.parse(startTime), sdf1.parse(endTime), statistical
							.getType(), null, null));
					statisticalList = statisticalService.list(sdf1
							.parse(startTime), sdf1.parse(endTime), statistical
							.getType(), null, null,productName, pg.getFirstResult(), pg
							.getPageSize());
					provinceList = provinceService.list();
				} else {
					List<Province> provinces = new ArrayList<Province>(user
							.getProvinces());

					Province province = provinces.get(0);
					provinceid=province.getId();
					if (province.getProvinceid() != null) {
						provinceList = provinceService.list(
								"from Province p where p.cityid=?1", province
										.getId().toString());
						epid=ProvinceUtil.getLastIndexOfCity(provinceService, provinceid);
						
					} else if (province.getCityid() != null) {
						epid=provinceid;
					} else {
						provinceList = provinceService.list(
								"from Province p where p.provinceid=?1",
								province.getId().toString());
						epid=ProvinceUtil.getLastIndexOfProvince(provinceService, provinceid);
					}
					pg.setRecordCount(statisticalService.getTotalCount(sdf1
							.parse(startTime), sdf1.parse(endTime), statistical
							.getType(), provinceid,epid));
					statisticalList = statisticalService.list(sdf1
							.parse(startTime), sdf1.parse(endTime), statistical
							.getType(), provinceid,epid,productName, pg
									.getFirstResult(), pg.getPageSize());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (statisticalList.size() <= 0) {
			this.addActionMessage("您查询的数据不存在!");
		}
		request.setAttribute("pagination", pg);
		if (statistical.getType().equals(StatisticalType.SALES)) {
			return "sales";
		}
		return SUCCESS;
	}

	// 查看统计详细
	public String getDetail() {
		statistical = statisticalService.find(statisticalid);

		if (statistical.getAreaType().equals(TongjiAreaType.STATELEVEL)) {
			statisticalList = statisticalService.listByProduct(statistical
					.getProduct().getId(), TongjiAreaType.COUNTYLEVEL,
					StatisticalType.PURCHASE, new Date());
		} else if (statistical.getAreaType().equals(TongjiAreaType.COUNTYLEVEL)) {
			statisticalList = statisticalService.listByProduct(statistical
					.getProduct().getId(), TongjiAreaType.CITYLEVEL,
					StatisticalType.PURCHASE, new Date());
		} else {
			orderList = new ArrayList<OrderForm>(statistical.getOrders());
			return "viewOrder";
		}
		return "view";
	}

	public Statistical getModel() {
		return statistical;
	}

	// PREPARABLE
	public void prepare() throws Exception {
		if (statisticalid == null || "".equals(statisticalid)) {
			statistical = new Statistical();
		} else {
			statistical = statisticalService.find(statisticalid);
		}
	}

	public Statistical getStatistical() {
		return statistical;
	}

	public void setStatistical(Statistical statistical) {
		this.statistical = statistical;
	}

	public List<Statistical> getStatisticalList() {
		return statisticalList;
	}

	public void setStatisticalList(List<Statistical> statisticalList) {
		this.statisticalList = statisticalList;
	}

	public Integer getStatisticalid() {
		return statisticalid;
	}

	public void setStatisticalid(Integer statisticalid) {
		this.statisticalid = statisticalid;
	}

	public List<Province> getProvinceList() {
		return provinceList;
	}

	public void setProvinceList(List<Province> provinceList) {
		this.provinceList = provinceList;
	}

	public Integer getProvinceid() {
		return provinceid;
	}

	public void setProvinceid(Integer provinceid) {
		this.provinceid = provinceid;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public Integer getProductid() {
		return productid;
	}

	public void setProductid(Integer productid) {
		this.productid = productid;
	}

	public List<OrderForm> getOrderList() {
		return orderList;
	}

	public void setOrderList(List<OrderForm> orderList) {
		this.orderList = orderList;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

}
